package com.txzt.gaea.business.project.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.project.domain.model.ProjectDevice;
import com.txzt.gaea.business.project.service.IProjectDeviceService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;

import com.txzt.gaea.common.enums.IsDeleteEnum;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/project/device")
public class ProjectDeviceController extends BaseController {


    private static final Logger logger = LoggerFactory.getLogger(ProjectDeviceController.class);

    @Autowired
    private IProjectDeviceService projectDeviceService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param projectDevice
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, ProjectDevice projectDevice) {
        logger.info(projectDevice.toString());
        PageQuery<ProjectDevice> pageList = projectDeviceService.pageQuery(new PageQuery<>(pageNumber, pageSize, projectDevice));
        pageList.getList().forEach(item -> {
            //翻译创建人
            item.setCreateName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译所属部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 新增
     * @param projectDevice
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody ProjectDevice projectDevice) {
        int count = projectDeviceService.insertTemplate(projectDevice);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 编辑
     * @param projectDevice
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody ProjectDevice projectDevice) {
        int count = projectDeviceService.updateById(projectDevice);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        ProjectDevice projectDevice = new ProjectDevice();
        projectDevice.setId(id);
        projectDevice.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = projectDeviceService.updateById(projectDevice);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

}
